package com.tzy.shopcascoupon.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tzy.common.constant.WebResult;
import com.tzy.common.constant.WebResultMsgCode;
import com.tzy.common.entity.ShopCasCoupon;
import com.tzy.shopcascoupon.service.ShopCasCouponService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商家代金券 前端控制器
 * </p>
 *
 * @author 刘同学
 * @since 2022-03-04
 */
@RestController
@RequestMapping("/shopcascoupon")
public class ShopCasCouponController {
    @Resource
    private ShopCasCouponService casCouponService;

    /**
     * 修改数据
     * @param shopCasCoupon 实体类对象
     * @return 返回受影响行数
     */
    @PutMapping("/cascouponupdate")
    public WebResult casCouponUpdate(@RequestBody ShopCasCoupon shopCasCoupon){

        int jg = casCouponService.update(shopCasCoupon);
        if(jg>0){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("cascouponupdate","修改成功");
        }
        return new WebResult(WebResultMsgCode.FAIL).putData("cascouponupdate","修改失败");
    }

    /**
     * 添加数据
     * @return 返回受影响行数
     */
    @GetMapping("/tianjia")
    public WebResult insert(@RequestParam Integer shopId,@RequestParam Integer threshold,@RequestParam Integer discount){
         ShopCasCoupon shopCasCoupon = new ShopCasCoupon();
         shopCasCoupon.setShopId(shopId);
         shopCasCoupon.setThreshold(threshold);
         shopCasCoupon.setDiscount(discount);
        boolean lean = casCouponService.save(shopCasCoupon);
        if (lean){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("couponAdd","添加成功");
        }
        return new WebResult(WebResultMsgCode.FAIL).putData("couponAdd","添加失败");
    }

    /**
     * 查询数据
     * @param shopId 商家id
     * @return 返回数据集
     */
    @PostMapping("/chaxunShopId")
    public WebResult selectShopId(@RequestParam Integer shopId){
        List<ShopCasCoupon> list= casCouponService.select(shopId);
        if (list!=null || list.isEmpty()){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("couponSelId",list);
        }
        return new WebResult(WebResultMsgCode.FAIL).putData("couponSelId","加载失败");
    }

    /**
     * 查询数据
     * @return 返回数据集
     */
    @GetMapping("/casCouponPage")
    public WebResult pageTest(@RequestParam(defaultValue = "1") Integer pageNum){
        Page<ShopCasCoupon> page = casCouponService.casCouponPage(pageNum, 5);
        return new WebResult(WebResultMsgCode.SUCCESS).putData("page",page);
    }

    @GetMapping("/casCouponTotal")
    public WebResult findTotal(){
        int jg = casCouponService.jiLuShu();
        if(jg>0) {
            return new WebResult(WebResultMsgCode.SUCCESS).putData("total", jg);
        }
        return new WebResult(WebResultMsgCode.FAIL).putData("total","加载失败");
    }

    /**
     * 删除数据
     * @param id 代金券id
     * @param shopId 商家id
     * @return 返回受影响行数
     */
    @DeleteMapping("/del")
    public WebResult casCouponDel(@RequestParam Integer id, @RequestParam Integer shopId){
        int jg = casCouponService.casCouponDel(id,shopId);
        if (jg>0){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("couponDel","删除成功");
        }
        return new WebResult(WebResultMsgCode.FAIL).putData("couponDel","删除失败");

    }
}
